<?php

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;

class IndexController extends AbstractActionController {

    public function indexAction() {
        $linksTmp = $this->getServiceLocator()->get('Application\Model\LinksTmp');

        return array();
    }

    public function categoryTempAction() {
        $cate = $url = $template = $spec = null;
        $request = $this->getRequest();
        if ($request->isPost()) {
            $filter = $this->getServiceLocator()->get('Crawler\Filter');
            $url = trim($request->getPost('url-links'));
            $template = trim($request->getPost('template'));
            $spec = trim($request->getPost('spec'));
            $cate = $filter->getLinkTemp($url, $template, $spec);
            $cate = json_decode($cate);
            if ($request->getPost('hasResult')) {
                $linksTmp = $this->getServiceLocator()->get('Application\Model\LinksTmp');
                $linksTmp->saveLinks(array(
                    'url_links' => $request->getPost('url_links'),
                    'prtt' => htmlentities($request->getPost('prtt')),
                    'filter_link' => $request->getPost('filter_link'),
                    'domain' => $request->getPost('domain'),
                ));
            }
        }
        return array(
            'url_links' => $url,
            'template' => htmlentities($template),
            'domain' => parse_url($url, PHP_URL_HOST),
            'spec' => $spec,
            'cate' => $cate,
        );
    }

    public function articleTempAction() {

        $request = $this->getRequest();
        if ($request->isXMLHttpRequest()) {

            $url = $request->getPost('url');
            $ptt = $request->getPost('ptt');
            $filter = $this->getServiceLocator()->get('Crawler\Filter');
            /* HTMLLoader */
            $content = $filter->downloadUrl($url);
            $type = $request->getPost('type');

            /* TYPEPatterm */
            if ($type == 'split')
                $result = $filter->getSubContent($content, $ptt);
            else
                $result = $filter->getGCContentNeedTemp($content, $ptt);

            $viewRenderer = $this->getServiceLocator()->get('Zend\View\Renderer\PhpRenderer');
            $resp = $viewRenderer->partial('partial/ajax-filter-artical', array('type' => $type, 'result' => json_decode($result)));

            return $this->getResponse()->setContent($resp);
        }

        if ($request->isPost()) {
            $obj = new \stdClass();
            $data = array();
            $p = (array) $request->getPost();
            $url = array_shift($p);
            $base_type = array('split', 'ptt');

            foreach ($p as $key => $item) {
                $item = trim($item);
                $tmp = json_decode($item);
                if (!in_array($tmp->type, $base_type))
                    die('Chuỗi prototype :' . $key . ' không hợp lệ');
                $obj->{$key} = $item;
            }

            $data['prtt'] = json_encode($obj);
            $data['url_article'] = urlencode($url);
            $data['domain'] = parse_url($url, PHP_URL_HOST);

            $articletmp = $this->getServiceLocator()->get('Application\Model\ArticleTmp');
            $articletmp->insert($data);
        }
    }

}
